﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.Common;
using System.IO;
using BarCode.Module;
using BarCode.AccessDao;
using System.Runtime.InteropServices;
using BarCode.Bean;

namespace BarCode.Forms
{
    public partial class ZKBBarCode : Form
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(Base_BarCode));


        [DllImport("user32.dll")]
        public static extern bool MessageBeep(uint uType);

        private int workAmount;

        public ZKBBarCode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_Load(object sender, EventArgs e)
        {
            lblPreUser.Text = "";
            lblPreUser.ForeColor = Color.Blue;
            lblUser.Text = GlobalVal.CurrentUser.User;
            txtBarCode.Focus();

            // 获取当前用户当天的工作量
            DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
            workAmount = db01.getMpWorkAmount(GlobalVal.CurrentUser.User );
            lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();

            txtBarCode.Text = "";
            txtBarCode.Focus();
            lblRepeat.Visible = false;
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Base_BarCode_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
        }


        /// <summary>
        /// 二维码输入后回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)13)
                {
                    lblPreUser.ForeColor = Color.Blue;
                    lblPreUser.Text = "";
                    string inputBarCode = txtBarCode.Text;
                    lblRepeat.Visible = false;
                    if (inputBarCode.Length != 12)
                    {
                        MessageUtil.Warn("条码长度不正确!");
                        txtBarCode.Text = "";
                        txtBarCode.Focus();
                        ComUtil.sendFail(workAmount);
                        return;
                    }

                    DB01Dao db01 = new DB01Dao(GlobalVal.serverMysqlInfo.MYSQL_IP, GlobalVal.serverMysqlInfo.USERID, GlobalVal.serverMysqlInfo.USERPWD);
                    // 查询是否重复
                    if (db01.isRepeat(inputBarCode))
                    {
                        lblRepeat.Visible = true;
                        ComUtil.sendFail(workAmount);
                    }
                    else
                    {
                        // 查询前道工序是否存在
                        string preUser = db01.getPreUser(inputBarCode);
                        if (string.IsNullOrEmpty(preUser))
                        {
                            lblPreUser.ForeColor = Color.Red;
                            lblPreUser.Text = "不存在";
                            ComUtil.sendFail(workAmount);
                        }
                        else
                        {
                            lblPreUser.ForeColor = Color.Blue;
                            lblPreUser.Text = preUser.ToString();

                            // 保存本道工序记录
                            db01.saveZKBBarCode(inputBarCode);

                            // 获取当前用户当天的工作量
                            workAmount = db01.getMpWorkAmount(GlobalVal.CurrentUser.User);
                            lblAmount.Text = (workAmount + GlobalVal.BASE_COUNT).ToString();

                            ComUtil.sendSuccess(workAmount);
                        }
                    }

                    // 清空输入内容
                    txtBarCode.Text = "";
                    txtBarCode.Focus();
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
            }
        }

        /// <summary>
        /// 输入有误时弹框显示
        /// </summary>
        /// <param name="msg"></param>
        private void ShowInputWarn(string msg, uint beep)
        {
            txtBarCode.Text = "";
            txtBarCode.Focus();
           
            if (beep > 0)
            {
                MessageBeep(beep);
            }
            MessageUtil.Warn(msg);
        }
    }
}
